1   package net.sf.fastxmldb.impl;
2   
3   import java.util.Collections;
4   import java.util.HashMap;
5   import java.util.Map;
6   import org.xmldb.api.base.Collection;
7   import org.xmldb.api.base.XMLDBException;
8   
9   /***
10   * The CollectionBroker is responsible for keeping track
11   * of all created collections and the default collection
12   * @author jbirchfield
13   */
14  public class CollectionBroker {
15  
16      private static CollectionBroker instance;
17  
18      private Map collections = null;
19  
20      /***
21       * 
22       * @return CollectionBroker the static instance
23       */
24      public static CollectionBroker getInstance() {
25          if (instance == null) {
26              instance = new CollectionBroker();
27          }
28          return instance;
29      }
30  
31      private CollectionBroker() {
32          collections = new HashMap(10);
33          Collections.synchronizedMap(collections);
34          createDefaultCollection();
35      }
36  
37      private void createDefaultCollection() {
38          Collection collection = new LocalCollection("default");
39          try {
40              addCollection(collection);
41          } catch (XMLDBException e) {
42              e.printStackTrace();
43          }
44      }
45  
46      /***
47       * 
48       * @param name the name of the collection
49       * @return Collection the Collection object
50       */
51      public Collection getCollection(String name) {
52          return (Collection) collections.get(name);
53      }
54  
55      /***
56       * 
57       * @param collection the collection to add
58       * @throws XMLDBException if an exception occurs during the 
59       * getName of a collection
60       */
61      public void addCollection(Collection collection) throws XMLDBException {
62          collections.put(collection.getName(), collection);
63      }
64  
65      /***
66       * 
67       * @return int the number of collections.  Should always be at least 1
68       */
69      public int getCollectionCount() {
70          return collections.size();
71      }
72  
73      /***
74       * Clear all the collections and recreate the default one
75       */
76      public void clearCollections() {
77          collections.clear();
78          createDefaultCollection();
79      }
80  }
This page was automatically generated by Maven